<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of SpecialAjaxController
 *
 * @author Ganda
 */
class SpecialAjaxController extends Controller{
    
    public function accessRules()
    {
            return array(
                array('allow',
                        'actions'=>array('checklist', 'getlocation', 'getshippingtype', 'getpacking', 'getasuransi'),
                        'users'=>array('@'),
                ),
            );
    }
    
    
    private function getAllowMyAccessRule($seachAction)
    {
        $boleh = FALSE;
        $myAccessRule = $this->accessRules();

        for($i = 0; $i< count($myAccessRule);$i++){
            $arr = $myAccessRule[$i];
            $arrAction = array();
            $arrUser = array();

            if(is_int(array_search('allow', $arr))){
                $arrAction = isset($arr['actions'])?$arr['actions']:array();
                $arrUser = isset($arr['users'])?$arr['users']:array();

                if(is_int(array_search($seachAction, $arrAction))){
                    if(is_int(array_search(Yii::app()->user->name, $arrUser))
                        || is_int(array_search(!Yii::app()->user->isGuest?'@':'', $arrUser))
                        || is_int(array_search(Yii::app()->user->isGuest?'*':'', $arrUser))
                        ){
                        $boleh = TRUE;
                    }
                }
            }
        }
                
        return $boleh;
    }
    
    public function actionChecklist(){
        $accRule = Sysappaccessrule::model()->findAll("MenuGroupID='{$_POST['menuGroupID']}' and PermissionAccessID = 1 and RuleTypeID = 1 and isActive = 1");
        
        $a = array();
        
        if($this->getAllowMyAccessRule(strtolower(str_replace('action', '', __FUNCTION__)))){
            foreach($accRule as $ar){
                array_push($a, array('ControllerName'=>$ar->ControllerName,'RuleValues'=>$ar->RuleValues));
            }
        }
        echo json_encode($a);
    }
    
    public function actionGetLocation(){
        $location = new Gflocation();
        $location = Gflocation::model()->find("locationID='{$_POST['locationID']}' and isActive = 1");
        
        $a = array();
        if($this->getAllowMyAccessRule(strtolower(str_replace('action', '', __FUNCTION__)))){
            array_push($a, array(
                'LocationID'=>$location->LocationID,
                'CountryID'=>$location->CountryID,
                'CountryName'=>$location->country->CountryName,
                'ProvinceID'=>$location->ProvinceID,
                'ProvinceName'=>$location->province->ProvinceName,
                'RegencyID'=>$location->RegencyID,
                'RegencyName'=>$location->regency->RegencyName,
                'VillageID'=>$location->VillageID,
                'VillageName'=>$location->village->VillageName,
                'ZipCode'=>$location->ZipCode));
        }
        echo json_encode($a);
    }
    
    public function actionGetShippingType(){
        $shipType = new Masteritemshippingtype();
        $shipType = Masteritemshippingtype::model()->find("ItemShippingTypeID='{$_POST['ItemShippingTypeID']}' and isActive = 1");
        
        $a = array();
        if($this->getAllowMyAccessRule(strtolower(str_replace('action', '', __FUNCTION__)))){
            array_push($a, array(
                'ItemShippingTypeID'=>$shipType->ItemShippingTypeID,
                'ItemShippingTypeName'=>$shipType->ItemShippingTypeName,
                'Cost'=>$shipType->Cost,
                'OtherCost'=>$shipType->OtherCost,
                ));
        }
        echo json_encode($a);
    }
    
    public function actionGetPacking(){
        $packing = new Masterpacking();
        $packing = Masterpacking::model()->find("PackingID='{$_POST['PackingID']}' and isActive = 1");
        
        $a = array();
        if($this->getAllowMyAccessRule(strtolower(str_replace('action', '', __FUNCTION__)))){
            array_push($a, array(
                'PackingID'=>$packing->PackingID,
                'PackingCost'=>$packing->PackingCost,
                ));
        }
        echo json_encode($a);
    }
    
    public function actionGetAsuransi(){
        $asuransi = new Insprograms();
        $asuransi = Insprograms::model()->find("InsuranceNumber='{$_POST['InsuranceNumber']}' and isActive = 1");
        
        $a = array();
        if($this->getAllowMyAccessRule(strtolower(str_replace('action', '', __FUNCTION__)))){
            array_push($a, array(
                'InsuranceNumber'=>$asuransi->InsuranceNumber,
                'InsuranceCost'=>$asuransi->InsuranceCost,
                'AdministrationFee'=>$asuransi->AdministrationFee,
                ));
        }
        echo json_encode($a);
    }
}

?>
